package com.usvisatrack.core.factories;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.usvisatrack.core.common.Factory;
import com.usvisatrack.core.dao.model.USEmbassy;
import com.usvisatrack.core.dao.model.User;
import com.usvisatrack.core.dao.model.Visa;

public class UserFactory {
	private static final int LISTING_PAGE_SIZE = 20;
	public static final int DEFAULT_AUTH_EXPIRATION = 10;

	private static Logger logger = LoggerFactory.getLogger(UserFactory.class);

	public static User get(Long id) {
		logger.debug("Get User OBJ by ID - [{}]", id);
		Factory.beginTransaction();
		User user = null;
		try {
			user = Factory.get(User.class, id);
			Factory.commitTransaction();
		} catch (Exception ex) {
			logger.error("Get User by user ID error", ex);
		} finally {
			Factory.rollbackTransaction();
		}

		return user;
	}

	/**
	 * Get user by userName
	 * 
	 * @param userName
	 * @return
	 */
	public static User get(String userName) {
		logger.debug("Search USEmbassy - [{}]/[{}]", userName);
		try {
			Factory.beginTransaction();
			Criteria criteria = Factory.createCriteria(User.class);
			criteria.add(Restrictions.eq("userName", userName));
			criteria.setMaxResults(1);
			return (User) criteria.uniqueResult();
		} catch (Exception ex) {
			logger.error("Search USEmbassy data error", ex);
		} finally {
			Factory.commitTransaction();
		}

		return null;

	}

	/**
	 * 
	 * @param user
	 */
	public static void save(User user) {
		try {
			Factory.beginTransaction();
			Factory.saveOrUpdate(user);
			Factory.commitTransaction();
		} catch (Exception ex) {
			logger.error("Save USER OBJ ERROR", ex);
			Factory.rollbackTransaction();
		}
	}
}